<?php

namespace app\api\model\kuyou\applet;

use app\api\controller\kuyou\applet\Commons;
use think\Model;
use fast\Tree;

class GoodsType extends Model
{

    // 表名
    protected $name = 'kuyou_goods_type';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';


    /**
     * 获取列表
     * @param array $where
     * @param int $page
     * @param int $limit
     * @return array
     */
    public static function getList($where,$page,$limit,$type){
        $app_page = config('app_page');
        $app_page = $app_page===false?false:true;
        $pageStatus = true;
        if($app_page){
            if($page<0){
                $pageStatus = false;
            }
        }
        if($type==1){
            if(!isset($where['pid'])){
                $where['pid'] = 0;
            }
        }
        if($pageStatus){
            $list = collection(self::where(function ($query) use ($where) {
                $query->where($where);
            })->page($page,$limit)->field('id,pid,supplier_type_name as name,supplier_type_image as image,status,FROM_UNIXTIME(createtime,\'%Y-%m-%d\') as createtime,FROM_UNIXTIME(updatetime,\'%Y-%m-%d\') as updatetime')->select())->toArray();
        }else{
            $list = collection(self::where(function ($query) use ($where) {
                $query->where($where);
            })->field('id,pid,supplier_type_name as name,supplier_type_image as image,status,FROM_UNIXTIME(createtime,\'%Y-%m-%d\') as createtime,FROM_UNIXTIME(updatetime,\'%Y-%m-%d\') as updatetime')->select())->toArray();
        }
        if($type==1){
            $map = '';
            foreach ($list as $list_k=>$list_v){
                if(!empty($map)){
                    $map .= ' or ';
                }
                $map .= " path like '%,".$list_v['id'].",%' ";
            }
            $pid = $where['pid'];
            unset($where['pid']);
            $lists = collection(self::where(function ($query) use ($map,$where) {
                $query->where($where);
                $query->where($map);
            })->field('id,pid,supplier_type_name as name,supplier_type_image as image,status')->select())->toArray();
            foreach ($lists as $lists_k=>$lists_v){
                $list[] =  $lists_v;
            }
            $tree = Tree::instance();
            $tree->init($list, 'pid');
            $list = $tree->getTreeArray($pid);
        }
        $count = self::where(function ($query) use ($where) {
            $query->where($where);
        })->count();
        return ['list'=>$list,'count'=>$count];
    }

    /**
     * 获取详情
     * @param int $id
     * @return array
     */
    public static function getDetail($id){
        $detail = self::where(function ($query) use ($id) {
            $query->where('id', '=', $id);
        })->field('id,pid,supplier_type_name as name,supplier_type_image as image,status')->find();
        if($detail){
            $detail['image'] = $detail['image'] ? cdnurl($detail['image'], true) : '';
            $detail['p_name'] = self::where(['id'=>$detail['pid']])->value('supplier_type_name');
        }
        return $detail;
    }

    /**
     * 添加
     * @param array $detail
     * @return array
     */
    public static function addDetail($detail){
        if(empty($detail)){
            return ['status'=>400,'msg'=>'参数错误'];
        }else{
            $check = (new Commons())->checkParameter($detail,[
                'pid|PID'=>'require|number',
                'supplier_type_name|名称'=>'require',
                'status|状态'=>'require|in:0,1',
            ]);
            if(isset($check['status'])){
                return $check;
            }else{
                if($detail['pid']>0){
                    $id = $detail['pid'];
                    $pid = self::where(function ($query) use ($id) {
                        $query->where('id', '=', $id);
                    })->field('id,path')->find();
                    if(empty($pid)){
                        return ['status'=>500,'msg'=>'上级分类不存在'];
                    }else{
                        $detail['path'] = $pid['path'].$pid['id'].',';
                    }
                }else{
                    $detail['path'] = ',0,';
                }
                $detail['createtime'] = time();
                $detail['updatetime'] = time();
                $res = (new GoodsType())->insertGetId($detail);
                if($res){
                    return ['status'=>200,'msg'=>$res];
                }else{
                    return ['status'=>500,'msg'=>'添加失败'];
                }
            }
        }
    }

    /**
     * 更新
     * @param array $detail
     * @return array
     */
    public static function editDetail($detail){
        if(empty($detail)){
            return ['status'=>400,'msg'=>'参数错误'];
        }else{
            $check = (new Commons())->checkParameter($detail,[
                'id|ID'=>'require|number',
                'pid|PID'=>'require|number',
                'supplier_type_name|分类名称'=>'require',
                'status|分类状态'=>'require|in:0,1',
            ]);
            if(isset($check['status'])){
                return $check;
            }else{
                if($detail['pid']>0){
                    $id = $detail['pid'];
                    $pid = self::where(function ($query) use ($id) {
                        $query->where('id', '=', $id);
                    })->field('id,path')->find();
                    if(empty($pid)){
                        return ['status'=>500,'msg'=>'上级分类不存在'];
                    }else{
                        $detail['path'] = $pid['path'].$pid['id'].',';
                    }
                }else{
                    $detail['path'] = ',0,';
                }
                $id = $detail['id'];unset($detail['id']);
                $detail['updatetime'] = time();
                $res = (new GoodsType())->where(['id'=>$id])->update($detail);
                if($res){
                    return ['status'=>200,'msg'=>'更新成功'];
                }else{
                    return ['status'=>500,'msg'=>'更新失败'];
                }
            }
        }
    }

    /**
     * 删除
     * @param int $id
     * @return array
     */
    public static function delDetail($id){
        if(empty($id)){
            return ['status'=>400,'msg'=>'参数错误'];
        }else{
            $ids = explode(',',$id);
            $map = '';
            foreach ($ids as $ids_k=>$ids_v){
                if(!empty($map)){
                    $map .= ' or ';
                }
                $map .= " path like '%,".$id.",%' ";
            }
            $children = collection(self::where(function ($query) use ($map) {
                $query->where($map);
            })->column('id'))->toArray();
            if(!empty($children)){
                return ['status'=>500,'msg'=>'此分类有下级分类。不可删除'];
            }

            $goods = (new Goods())->where(['goods_type_id'=>['in',$id]])->find();
            if($goods){
                return ['status'=>500,'msg'=>'此分类下有商品数据。不可删除'];
            }
            $res = (new GoodsType())->where(['id'=>['in',$id]])->delete();
            if($res){
                return ['status'=>200,'msg'=>$res];
            }else{
                return ['status'=>500,'msg'=>'删除失败'];
            }
        }
    }
}
